package com.accessibleworlds.blind;

import java.util.ArrayList;

import com.accessibleworlds.R;
import com.accessibleworlds.gateway.Main;
import com.accessibleworlds.util.SharedVariables;
import com.accessibleworlds.util.TextToSpeeches;
import com.accessibleworlds.util.TextToSpeechesGoogle;
import com.accessibleworlds.util.TextToSpeechesPico;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;

/**
 * Represents the main screen class for a blind user.
 * @author Ayberk Özgür
 *
 */
public class BlindUserMainPage extends Activity implements MultitouchGestureListener {
	private TextToSpeeches tts = null;
	private boolean exitRequested = false;
	private boolean ttsReady = false;
	private Thread connectionThread;
	private ArrayList<BlindUserMenuItem> spokenMenu = null;
	private int currentItem = 0;
	private int numberOfItems = 4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.blinduserpage);

		//Populate the spoken menu
		spokenMenu = new ArrayList<BlindUserMenuItem>();
		Intent intent;
		intent = new Intent(this,BlindUserWalkingMode.class);
		spokenMenu.add(new BlindUserMenuItem(this,getResources().getStringArray(R.array.blind_main_items)[0],intent,true));
		intent = new Intent(this,BlindUserSearchReports.class);
		spokenMenu.add(new BlindUserMenuItem(this,getResources().getStringArray(R.array.blind_main_items)[1],intent,true));
		intent = new Intent(this,BlindUserCreateReportPage.class);
		spokenMenu.add(new BlindUserMenuItem(this,getResources().getStringArray(R.array.blind_main_items)[2],intent,true));
		spokenMenu.add(new BlindUserMenuItem(this,getResources().getStringArray(R.array.blind_main_items)[3],null,false));		
		connectionThread = new Thread(new Runnable() {
			public void run() {
				connection();
			}
		});
		connectionThread.start();
	}

	@Override
	public void onBackPressed() {
	}

	private void connection() {
		if("0".equalsIgnoreCase(getResources().getStringArray(R.array.languageValues)[SharedVariables.currentLanguage]))
			tts = new TextToSpeechesPico();
		else
			tts = new TextToSpeechesGoogle();
		tts.speak(BlindUserMainPage.this, getResources().getString(R.string.blind_main) + "Current item: " + spokenMenu.get(currentItem).getSpokenName());
		ttsReady = true;
	}

	public void multitouchMotionEvent(int numberOfFingers, GestureType gestureType) {
		if(ttsReady){
			tts.stopSpeaking();
			switch(gestureType){
			case DOWN:
				exitRequested = false;
				currentItem = (currentItem + 1) % numberOfItems;
				tts.speak(BlindUserMainPage.this, spokenMenu.get(currentItem).getSpokenName());
				break;
			case UP:
				exitRequested = false;
				currentItem--;
				if(currentItem < 0)
					currentItem = numberOfItems - 1;
				tts.speak(BlindUserMainPage.this, spokenMenu.get(currentItem).getSpokenName());
				break;
			case LEFT:
				if(exitRequested)
					finish();
				else{
					exitRequested = true;
					tts.speak(BlindUserMainPage.this, getResources().getString(R.string.blind_exit_requested));
				}
				break;
			case RIGHT:
				exitRequested = false;
				if(currentItem == 3){
					Intent intent = new Intent(BlindUserMainPage.this,Main.class);

					SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
					SharedPreferences.Editor editor = preferences.edit();
					editor.putBoolean("autoLogin", false);
					editor.putString("email", "");
					editor.putString("password", "");
					editor.putBoolean("blindUserMode", true);
					editor.commit();

					SharedPreferences savePreferences = getSharedPreferences(SharedVariables.fileName, MODE_PRIVATE);
					SharedPreferences.Editor saveEditor = savePreferences.edit();
					saveEditor.putString("userID", "");
					saveEditor.putString("email", "");
					saveEditor.putString("password", "");
					saveEditor.putString("lastName", "");
					saveEditor.putString("firstName", "");
					saveEditor.putString("birthday", "");
					saveEditor.commit();
					
					startActivity(intent);
					
					finish();
				}
				else
					spokenMenu.get(currentItem).selectItem();
				break;
			}
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		tts.speak(this, "This is the main page. Current item: " + spokenMenu.get(currentItem).getSpokenName());
		super.onActivityResult(requestCode, resultCode, data);
	}
}
